#include "file_del.h"
#include "global.h"
//extern u8 idata device_active;
//extern DIR _xdata path_dj[8];
//extern FSAPIMSG _pdata fs_msg;
extern FATFS _xdata fat_fs;
extern u8 xdata vm_buffer[] ;
extern u8 _xdata id3_tag_buf[128];
extern u8 xdata id3_tag_len;

u8 delete_file()
{	
	u8 delete=0;
	u8 timeout=0;


	u8 msg;
	fscan_init(vm_buffer);	
	if(scan_fat.fs_type != FS_FAT16 && scan_fat.fs_type!=FS_FAT32)
	{
		return 0;
	}
	clear_lcd();	
	ResShowMultiString(16 , 0,cdelfile ); 

	
	ResShowPic(DELLA , 0 , 0);	
	show_file_name(0 , 2);

	while(1)
	{
		if(delete == 0)
			ResShowPic(CDEL_NO , 50 , 32);	
		else
			ResShowPic(CDEL_YES , 50 , 32);
		draw_lcd(0,8);
		msg = app_get_msg();
		switch(msg)
		{
			case MSG_KEY_PREV:
		 	case MSG_KEY_NEXT:
				 timeout = 0;
				 delete = (delete==0)? 1 : 0;
				 break;

			case MSG_KEY_PP:
				if(delete)
				{
					stop_decode();
                    cipher_ctl(0);
					clear_lcd();
                    
                	if((LanguageMode==Chinese_Simplified)||(LanguageMode==Chinese_Traditional))	
                		ResShowPic(CHNDEL,0,16);
                	else
                		ResShowPic(ENGDELLA,0,2*8);              

					draw_lcd(0,8);		
					clear_dir();
					clear_fat_table();	
					return 1;
				}
				return 0;

            case MSG_100MS:
                break;

            case MSG_200MS:
                break;

			case MSG_HALF_SECOND:
                if(displayinitok)
                    lcd_roll_disp(0x81);//disp_roll_ctrl(1);
                else
                    lcd_roll_disp(1);

                if(timeout < MENUWAITTIME)
					timeout++;
				else
					return 0;
				break;
			case MSG_USB_DISK_OUT:			
			case MSG_SDMMC_IN :
				given_device = DEVICE_SDMMC0;
				goto _EXIT_;
			case MSG_SDMMC_OUT:
			case MSG_USB_DISK_IN :
				given_device = DEVICE_UDISK;
				goto _EXIT_;

            case MSG_USB_PC_IN:
    			if(g_taskexistbit&BIT(TASK_UDISK_APP))
    			{
    			//	put_msg_lifo(MSG_CHANGE_WORK_MODE);
    				work_mode = TASK_UDISK_APP;
                    return 2;
    			}
			break;
			default:break;
		
		}				
	}
_EXIT_:
	given_file_method = PLAY_BREAK_POINT;
	put_msg_lifo(MSG_MUSIC_SELECT_NEW_DEVICE);		
   	return 0;
}

u8 delete_all_files()
{	
	u8 delete=0;
	u8 timeout=0;
	u8 msg;
	fscan_init(vm_buffer);	
	if(scan_fat.fs_type != FS_FAT16 && scan_fat.fs_type!=FS_FAT32)
	{
		return 0;
	}
	clear_lcd();	
	ResShowMultiString( 16 , 0,cdelall ); 
	ResShowPic(DELLA , 0 , 0);
	while(1)
	{
		if(delete == 0)
			ResShowPic(CDEL_NO , 50 , 32);	
		else
			ResShowPic(CDEL_YES , 50 , 32);
		draw_lcd(0,8);
		msg = app_get_msg();
		switch(msg)
		{
			case MSG_KEY_PREV:
		 	case MSG_KEY_NEXT:
				 timeout = 0;
				 delete = (delete==0)? 1 : 0;
				 break;

			case MSG_KEY_PP:
				if(delete)
				{
					stop_decode();
                    cipher_ctl(0);
					clear_lcd();
					if((LanguageMode==Chinese_Simplified)||(LanguageMode==Chinese_Traditional))	
                		ResShowPic(CHNDEL,0,16);
                	else
                		ResShowPic(ENGDELLA,0,2*8); 
					draw_lcd(0,8);
					clear_all_file();
					return 1;
				}
				return 0;

            case MSG_100MS:
            case MSG_200MS:
                 break;

			case MSG_HALF_SECOND:
				if(timeout < MENUWAITTIME)
					timeout++;
				else
					return 0;
				break;
			case MSG_USB_DISK_OUT:			
			case MSG_SDMMC_IN :
				given_device = DEVICE_SDMMC0;
				goto _EXIT_;
			case MSG_SDMMC_OUT:
			case MSG_USB_DISK_IN :
				given_device = DEVICE_UDISK;
				goto _EXIT_;

            case MSG_USB_PC_IN:
    			if(g_taskexistbit&BIT(TASK_UDISK_APP))
    			{
    				//put_msg_lifo(MSG_CHANGE_WORK_MODE);
    				work_mode = TASK_UDISK_APP;
                    return 2;
    			}
			break;

			default:break;
		
		}				
	 }
_EXIT_:
	given_file_method = PLAY_BREAK_POINT;
	put_msg_lifo(MSG_MUSIC_SELECT_NEW_DEVICE);		
   	return 0;

}